﻿
using System;
using System.Data;
using System.Text;

using System.Collections.Generic;
using System.Linq;
using WinnerCMS.Common;
using WinnerCMS.Model;
using System.Data.Entity;
namespace WinnerCMS.Data
{
    /// <summary>
    /// 数据访问层CMS_UDT。
    /// </summary>
    public class UDTDal
    {
        #region  添加数据
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="model"></param>
        public void Add(UDT model)
        {
            using (Entities db = new Entities())
            {
                db.UDTs.Add(model);
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  修改数据
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="model"></param>
        public void Edit(UDT model)
        {
            using (Entities db = new Entities())
            {
                db.UDTs.Attach(model);
                db.Entry(model).State = EntityState.Modified;
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="Ids"></param>
        public void Delete(int[] Ids)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.UDTs where Ids.Contains(x.Id) select x;
                q.ToList().ForEach(x => db.UDTs.Remove(x));
                db.SaveChangesAsync().Wait();
            }
        }

        #endregion

        #region  加载数据
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public UDT GetInfo(int Id)
        {
            using (Entities db = new Entities())
            {
                return db.UDTs.Where(x => x.Id == Id).FirstOrDefault();
            }
        }
        #endregion

        #region 取得智能标签
        /// <summary>
        /// 取得智能标签
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public UDT GetTag(string name)
        {
            //IDataReader dr = SQLBaseV2.ExecuteReader("select Id,Name,DataType,DataSource,SqlScript,Type,JsPath,IsStop,URLType,ShortTitle,Length,ItemTemplet,EmptyTemplet,Remark,CheckInDate,Tag from CMS_UDT where Name = @Name and IsStop = 0", arrParameter);

            using (Entities db = new Entities())
            {
                return db.UDTs.Where(x => x.Name == name && x.Stop == 0).FirstOrDefault();
            }
        }
        #endregion

        #region 取得智能标签
        /// <summary>
        /// 取得智能标签
        /// </summary>
        /// <param name="Type"></param>
        /// <returns></returns>
        public IList<UDT> GetTag(int Type)
        {
            //IDataReader dr = SQLBaseV2.ExecuteReader("select Id,Name,DataType,DataSource,SqlScript,Type,JsPath,IsStop,URLType,ShortTitle,Length,ItemTemplet,EmptyTemplet,Remark,CheckInDate,Tag from CMS_UDT where Type = @Type and IsStop = 0", arrParameter);

            using (Entities db = new Entities())
            {
                return db.UDTs.Where(x => x.Type == Type && x.Stop == 0).ToList();
            }
        }
        #endregion

        #region 停用或者启用
        /// <summary>
        /// 停用或者启用
        /// </summary>
        /// <param name="Ids"></param>
        public void StopOrRunit(string Ids)
        {
            using(Entities db = new Entities())
            {
                db.Database.ExecuteSqlCommand("update CMS_UDT set IsStop=1-IsStop where Id in (" + Ids + ")");
            }
        }
        
        #endregion

        #region 绑定分页
        /// <summary>
        /// 绑定分页
        /// </summary>
        /// <param name="sort"></param>
        /// <param name="keyword"></param>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList< UDT> BindList(string sort, string keyword, PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                var query = from x in db.UDTs select x;
                switch (sort)
                {
                    case "Name": query = query.Where(x => x.Name.StartsWith(keyword));
                        break;
                    case "Remark": query = query.Where(x => x.Remark.StartsWith(keyword));
                        break;
                    case "": 
                        //故意跳过
                        break;
                    default :
                        throw new NotSupportedException("参数不支持");
                }

                pp.RecordCount = query.Count();
                query = query.OrderByDescending(x => x.Id).Skip(pp.Skip).Take(pp.PageSize);

                return query.ToList(); 
            }
        }
        
        #endregion
    }
}